package com.fengwei.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fengwei.form.MapForObject.MapForObject;
import com.fengwei.requestdata.user.MdmsUserLogPagingListData;
import com.fengwei.service.ConsoleCommonService;
import com.fengwei.service.MdmsUserLogService;
import com.fengwei.util.PltResult;
import com.fengwei.util.ResponseUtil;
import com.fengwei.verify.Verify;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Classname MdmsUserLogController
 * @Description 操作记录
 * @Date 2020/11/24 17:57
 * @Author ludexin
 */
@Controller
public class MdmsUserLogController {
    private final static Logger logger = LoggerFactory.getLogger(MdmsUserLogController.class);

    @Autowired
    private MdmsUserLogService mdmsUserLogService;

    @Autowired
    private ConsoleCommonService consoleCommonService;


    @GetMapping(value = "/mdmsUserLog/loadPage")
    public String loadPage(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        return "mdmsUserLog";
    }

    /**
     * 操作记录查询
     */
    @PostMapping(path = "/mdmsUserLog/pagingList")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN','DBA')")
    public Map<String, Object> pagingList(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsUserLogPagingListData mdmsUserLogPagingListData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(), new TypeReference<>() {
        });

        // 校验传入参数
        Verify.verifyData(mdmsUserLogPagingListData);
        // 正式处理业务
        Map<String, Object> data = mdmsUserLogService.pagingList(mdmsUserLogPagingListData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

}
